/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
	"k8s.io/utils/ptr"
	"sigs.k8s.io/controller-runtime/pkg/conversion"

	clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
	ipamv1 "sigs.k8s.io/cluster-api/api/ipam/v1beta2"
)

func (src *IPAddress) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*ipamv1.IPAddress)

	return Convert_v1beta1_IPAddress_To_v1beta2_IPAddress(src, dst, nil)
}

func (dst *IPAddress) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*ipamv1.IPAddress)

	return Convert_v1beta2_IPAddress_To_v1beta1_IPAddress(src, dst, nil)
}

func (src *IPAddressClaim) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*ipamv1.IPAddressClaim)

	return Convert_v1beta1_IPAddressClaim_To_v1beta2_IPAddressClaim(src, dst, nil)
}

func (dst *IPAddressClaim) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*ipamv1.IPAddressClaim)

	return Convert_v1beta2_IPAddressClaim_To_v1beta1_IPAddressClaim(src, dst, nil)
}

func Convert_v1beta2_IPAddressClaimStatus_To_v1beta1_IPAddressClaimStatus(in *ipamv1.IPAddressClaimStatus, out *IPAddressClaimStatus, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_IPAddressClaimStatus_To_v1beta1_IPAddressClaimStatus(in, out, s); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta2 conditions should not be automatically be converted into legacy conditions (v1beta1).
	out.Conditions = nil

	// Retrieve legacy conditions (v1beta1) from the deprecated field.
	if in.Deprecated != nil && in.Deprecated.V1Beta1 != nil {
		if in.Deprecated.V1Beta1.Conditions != nil {
			clusterv1beta1.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1beta1_Conditions(&in.Deprecated.V1Beta1.Conditions, &out.Conditions)
		}
	}

	// Move new conditions (v1beta2) to the v1beta2 field.
	if in.Conditions == nil {
		return nil
	}
	out.V1Beta2 = &IPAddressClaimV1Beta2Status{}
	out.V1Beta2.Conditions = in.Conditions
	return nil
}

func Convert_v1beta1_IPAddressClaimStatus_To_v1beta2_IPAddressClaimStatus(in *IPAddressClaimStatus, out *ipamv1.IPAddressClaimStatus, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta1_IPAddressClaimStatus_To_v1beta2_IPAddressClaimStatus(in, out, s); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta1 conditions should not be automatically be converted into v1beta2 conditions.
	out.Conditions = nil

	// Retrieve new conditions (v1beta2) from the v1beta2 field.
	if in.V1Beta2 != nil {
		out.Conditions = in.V1Beta2.Conditions
	}

	// Move legacy conditions (v1beta1) to the deprecated field.
	if in.Conditions == nil {
		return nil
	}
	if out.Deprecated == nil {
		out.Deprecated = &ipamv1.IPAddressClaimDeprecatedStatus{}
	}
	if out.Deprecated.V1Beta1 == nil {
		out.Deprecated.V1Beta1 = &ipamv1.IPAddressClaimV1Beta1DeprecatedStatus{}
	}
	if in.Conditions != nil {
		clusterv1beta1.Convert_v1beta1_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&in.Conditions, &out.Deprecated.V1Beta1.Conditions)
	}
	return nil
}

func Convert_v1_TypedLocalObjectReference_To_v1beta2_IPPoolReference(in *corev1.TypedLocalObjectReference, out *ipamv1.IPPoolReference, _ apimachineryconversion.Scope) error {
	out.Kind = in.Kind
	out.Name = in.Name
	out.APIGroup = ptr.Deref(in.APIGroup, "")
	return nil
}

func Convert_v1beta2_IPPoolReference_To_v1_TypedLocalObjectReference(in *ipamv1.IPPoolReference, out *corev1.TypedLocalObjectReference, _ apimachineryconversion.Scope) error {
	out.Kind = in.Kind
	out.Name = in.Name
	if in.APIGroup != "" {
		out.APIGroup = ptr.To(in.APIGroup)
	}
	return nil
}

func Convert_v1_LocalObjectReference_To_v1beta2_IPAddressReference(in *corev1.LocalObjectReference, out *ipamv1.IPAddressReference, _ apimachineryconversion.Scope) error {
	out.Name = in.Name
	return nil
}

func Convert_v1beta2_IPAddressReference_To_v1_LocalObjectReference(in *ipamv1.IPAddressReference, out *corev1.LocalObjectReference, _ apimachineryconversion.Scope) error {
	out.Name = in.Name
	return nil
}

func Convert_v1_LocalObjectReference_To_v1beta2_IPAddressClaimReference(in *corev1.LocalObjectReference, out *ipamv1.IPAddressClaimReference, _ apimachineryconversion.Scope) error {
	out.Name = in.Name
	return nil
}

func Convert_v1beta2_IPAddressClaimReference_To_v1_LocalObjectReference(in *ipamv1.IPAddressClaimReference, out *corev1.LocalObjectReference, _ apimachineryconversion.Scope) error {
	out.Name = in.Name
	return nil
}

func Convert_v1beta1_IPAddressSpec_To_v1beta2_IPAddressSpec(in *IPAddressSpec, out *ipamv1.IPAddressSpec, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta1_IPAddressSpec_To_v1beta2_IPAddressSpec(in, out, s); err != nil {
		return err
	}
	out.Prefix = ptr.To(int32(in.Prefix))
	return nil
}

func Convert_v1beta2_IPAddressSpec_To_v1beta1_IPAddressSpec(in *ipamv1.IPAddressSpec, out *IPAddressSpec, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_IPAddressSpec_To_v1beta1_IPAddressSpec(in, out, s); err != nil {
		return err
	}
	out.Prefix = int(ptr.Deref(in.Prefix, 0))
	return nil
}

// Implement local conversion func because conversion-gen is not aware of conversion func in other packages (see https://github.com/kubernetes/code-generator/issues/94)

func Convert_v1_Condition_To_v1beta1_Condition(in *metav1.Condition, out *clusterv1beta1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1beta1.Convert_v1_Condition_To_v1beta1_Condition(in, out, s)
}

func Convert_v1beta1_Condition_To_v1_Condition(in *clusterv1beta1.Condition, out *metav1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1beta1.Convert_v1beta1_Condition_To_v1_Condition(in, out, s)
}
